<!DESCRIPTION>A script that only allows the user to submit a form once. Compatible with Microsoft FrontPage's built-in validation methods, which other 'Submit Once' scripts are not. It spoofs FrontPage by dynamically re-writing the onSubmit handler in the user's browser. <!/DESCRIPTION>
<!CATEGORY>Forms<!/CATEGORY>
<!SCRIPT>
<!-- START OF SCRIPT -->
<!-- HOW TO INSTALL SUBMIT ONCE FOR FRONTPAGE:
1. Copy code into the HEAD section of document
2. Add the onLoad event handler into the BODY tag -->
<!-- STEP ONE: Add code into HEAD section of document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function hijackValidator() {
// loop through all the Forms in the document
for (i=0; i<document.forms.length; i++) {
/* create a new property of the Form that tracks whether the Form has been submitted*/
document.forms[i].notSubmittedYet = true;
if (document.forms[i].onsubmit) {
var oldHandler = new String(document.forms[i].onsubmit)
/* oldHandler will be something like this, depending on the browser in use,
* We need to extract just the "FrontPage_Form1_Validator(this)" part
*/
//find "Validator"
var validatorIndex = oldHandler.indexOf("Validator");
//if "Validator" not found, then abort
if (validatorIndex<0) return;
//find space before name of validator function
var startIndex = oldHandler.lastIndexOf(" ",validatorIndex)
// if space not found, abort
if (startIndex<0) return;
// find final parenthesis
var endIndex = oldHandler.indexOf(")",validatorIndex);
//if parenthesis not found, abort
if (endIndex<0) return;
// create a String for the name of the validator function,
// and store it as a new property of the Form
document.forms[i].oldValidator = new String(oldHandler.slice(startIndex,endIndex+1));
/* oldValidator is something like this:
* "FrontPage_Form1_Validator(this);"
*/
}
else { //Form did not have an onSubmit handler previously
document.forms[i].oldValidator = new String("true;");
}
//substitute our own onSubmit handler for the one FrontPage created
document.forms[i].onsubmit = enhancedValidator;
} //end for
} //end hijackValidator()
function enhancedValidator() {
/* This is our new validator function.
* It first calls the FrontPage validator for the Form.
* If that returns TRUE, it then checks whether the Form
* has already been submitted.
* If it has been, we do not submit it again.
*/
if (eval(this.oldValidator.valueOf())) { // is FrontPage validation successful?
if (this.notSubmittedYet) { //did user click Submit already?
this.notSubmittedYet = false;
return true; // submit form
}
else { // form already submitted; complain to user
alert("You have already submitted the form. Please be patient. \nThe Internet has had a busy day and is a little tired, but as soon as it catches its breath, it will deliver your confirmation message.");
return false; // don't submit form
}
}
else { // FrontPage validator found error
return false; // don't submit form
}
} // end enhancedValidator()
// End -->
</script>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
<BODY onLoad="hijackValidator()">
->
<!-- END OF SCRIPT -->
<!/SCRIPT>
<!PREVIEW>
<!-- START OF SCRIPT -->
<!-- HOW TO INSTALL SUBMIT ONCE FOR FRONTPAGE:
1. Copy code into the HEAD section of document
2. Add the onLoad event handler into the BODY tag -->
<!-- STEP ONE: Add code into HEAD section of document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function hijackValidator() {
// loop through all the Forms in the document
for (i=0; i<document.forms.length; i++) {
/* create a new property of the Form that tracks whether the Form has been submitted*/
document.forms[i].notSubmittedYet = true;
if (document.forms[i].onsubmit) {
var oldHandler = new String(document.forms[i].onsubmit)
/* oldHandler will be something like this, depending on the browser in use,
* We need to extract just the "FrontPage_Form1_Validator(this)" part
*/
//find "Validator"
var validatorIndex = oldHandler.indexOf("Validator");
//if "Validator" not found, then abort
if (validatorIndex<0) return;
//find space before name of validator function
var startIndex = oldHandler.lastIndexOf(" ",validatorIndex)
// if space not found, abort
if (startIndex<0) return;
// find final parenthesis
var endIndex = oldHandler.indexOf(")",validatorIndex);
//if parenthesis not found, abort
if (endIndex<0) return;
// create a String for the name of the validator function,
// and store it as a new property of the Form
document.forms[i].oldValidator = new String(oldHandler.slice(startIndex,endIndex+1));
/* oldValidator is something like this:
* "FrontPage_Form1_Validator(this);"
*/
}
else { //Form did not have an onSubmit handler previously
document.forms[i].oldValidator = new String("true;");
}
//substitute our own onSubmit handler for the one FrontPage created
document.forms[i].onsubmit = enhancedValidator;
} //end for
} //end hijackValidator()
function enhancedValidator() {
/* This is our new validator function.
* It first calls the FrontPage validator for the Form.
* If that returns TRUE, it then checks whether the Form
* has already been submitted.
* If it has been, we do not submit it again.
*/
if (eval(this.oldValidator.valueOf())) { // is FrontPage validation successful?
if (this.notSubmittedYet) { //did user click Submit already?
this.notSubmittedYet = false;
return true; // submit form
}
else { // form already submitted; complain to user
alert("You have already submitted the form. Please be patient. \nThe Internet has had a busy day and is a little tired, but as soon as it catches its breath, it will deliver your confirmation message.");
return false; // don't submit form
}
}
else { // FrontPage validator found error
return false; // don't submit form
}
} // end enhancedValidator()
// End -->
</script>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->